package com.bookocean.infrastructure.persistence.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.bookocean.infrastructure.persistence.po.UserBadgePO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 用户徽章获得记录 Mapper 接口
 * 
 * @author BookOcean
 * @since 2024-01-01
 */
@Mapper
public interface UserBadgeMapper extends BaseMapper<UserBadgePO> {

    /**
     * 查询用户已获得的徽章
     * 
     * @param userId 用户ID
     * @return 用户徽章记录列表
     */
    @Select("SELECT ub.*, bd.name, bd.description, bd.icon_url, bd.category " +
            "FROM user_badge_po ub " +
            "JOIN badge_definition_po bd ON ub.badge_id = bd.badge_id " +
            "WHERE ub.user_id = #{userId} AND ub.is_visible = 1 " +
            "ORDER BY ub.earned_at DESC")
    List<UserBadgePO> selectUserBadges(@Param("userId") Long userId);

    /**
     * 查询用户是否已获得指定徽章
     * 
     * @param userId 用户ID
     * @param badgeId 徽章ID
     * @return 徽章记录
     */
    @Select("SELECT * FROM user_badge_po WHERE user_id = #{userId} AND badge_id = #{badgeId}")
    UserBadgePO selectUserBadge(@Param("userId") Long userId, @Param("badgeId") Long badgeId);

    /**
     * 统计用户徽章数量
     * 
     * @param userId 用户ID
     * @return 徽章数量
     */
    @Select("SELECT COUNT(*) FROM user_badge_po WHERE user_id = #{userId} AND is_visible = 1")
    Integer countUserBadges(@Param("userId") Long userId);
}
